<template>
  <el-container>
    <!--头部-->
    <el-header>
      <div class="logo">
        <img src="../../assets/imgs/ava.gif" alt="" />
      </div>

      <div class="title">新冠物资管理系统</div>
      <div class="operation">
        <el-dropdown trigger="click" @command="handleCommand">
          <span class="el-dropdown-link">
            <img src="../../assets/imgs/ava.gif" alt="" />
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item icon="el-icon-s-home" command="home">系统首页</el-dropdown-item>
            <el-dropdown-item icon="el-icon-paperclip" command="xx">交流讨论</el-dropdown-item>
            <el-dropdown-item icon="el-icon-switch-button" command="loginout">退出登入</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </el-header>
    <el-container class="home-container">
      <!--侧边栏-->
      <el-aside :width="isCollapse == true ? '64px' : '200px'">
        <div @click="IsCollapse" class="toggle-btn">|||</div>

        <!--侧边栏控件-->
        <el-menu default-active="2" class="el-menu-vertical-demo" background-color="#272c33" text-color="#fff"
          active-text-color="#ffd04b" :collapse="isCollapse" :collapse-transition="false" :router="true">
          <!--侧边栏组件-->
          <Menu :menuList="menuList"></Menu>
        </el-menu>
      </el-aside>
      <!--主体-->
      <el-main>
        <!--主体路由展示地方-->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
  import { mapMutations } from "vuex";
  import { USERLOGIN, USERLOGOUT } from "../../store/types";
  //引入侧边栏组件
  import Menu from "../../components/menu/MenuTree";
  //引入菜单树的请求方法
  import { tree } from "../../api/menu";
  export default {
    //属性参数
    data() {
      return {
        //菜单栏伸缩属性
        isCollapse: false,
        //菜单树集合
        menuList: [],
      };
    },
    //组件
    components: {
      //侧边栏组件注册
      Menu,
    },
    methods: {
      ...mapMutations([USERLOGIN, USERLOGOUT]),
      //头像下拉菜单点击事件
      handleCommand(command) {
        if (command === "home") {
        } else if (command === "xx") {
        } else {
          this[USERLOGOUT]();
          this.$router.push("/login");
        }
      },
      //菜单栏伸缩方法
      IsCollapse() {
        this.isCollapse = !this.isCollapse;
      },
      //获取菜单树
      async getMenuTree() {
        let result = await tree();
        this.menuList = result.data.tree;
        console.log(result);
      },
    },

    //创建之后的钩子函数
    created() {
      //调用菜单树的请求方法，拿到菜单树里的所有数据
      this.getMenuTree();
    },
  };
</script>

<style lang="less" scoped>
  .el-container {
    height: 100%;
  }

  .el-header {
    background-color: #272c33;
    color: #333;
    text-align: center;
    line-height: 60px;
    display: flex;
    align-items: center;
    justify-content: space-between;

    div {
      height: 60px;
    }

    img {
      width: 60px;
      height: 60px;
      border-radius: 50%;
    }

    .title {
      font-size: 26px;
      color: #ffffff;
      font-weight: bold;
      font-family: "楷体";
    }

    .logo {
      margin-left: 20px;
    }

    .operation {
      margin-right: 20px;
    }
  }

  .home-container {
    height: 100% !important;
    width: 100%;
  }

  .el-aside {
    background-color: #272c33;

    .el-menu-vertical-demo {
      border: 0;
    }

    .toggle-btn {
      background-color: #2d3a4b !important;
      line-height: 24px;
      font-size: 10px;
      color: #fff;
      text-align: center;
      letter-spacing: 0.2em;
      cursor: pointer;
    }
  }

  .el-main {
    background-color: #eaedf1;
  }
</style>